package yxy.game.pm2.active.command.barrierfund;

import org.joda.time.DateTime;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actfundreward;
import yxy.cherry.data.bean.task;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_关卡基金;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.barrierfund.BarrierFund;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;

@MessageType(proto = Command.ActBarrierFundDrawReward, classtype = ActiveMessage.ActBarrierFundDrawRewardRequest.class)
public class CMD_Active_关卡基金_DrawReward extends Command<ActiveMessage.ActBarrierFundDrawRewardRequest> {

	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActBarrierFundDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		int type = request.getType();
		int id = request.getId();
		ActiveMessage.ActBarrierFundDrawRewardResponse.Builder response = ActiveMessage.ActBarrierFundDrawRewardResponse.newBuilder();
		
		ActiveModule_关卡基金 am = ActiveModule_关卡基金.get(pcode,false);
		if(am == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		BarrierFund bf = am.getbfbytype(type);
		if(bf == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		DateTime buy = bf.getBuytime();
		if(bf.getDrawids().contains(id) && buy == null) {	//没买 && 免费已领取
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		if(bf.getDrawids().contains(id) && bf.getPaydrawids().contains(id) && buy != null) {	//已买 && 免费已领取 && 付费已领取
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		if(!bf.getIdcodemap().containsKey(id)) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		String code = bf.getIdcodemap().get(id);
		Task taskbean = Task.get(pcode, code);
		if(!taskbean.isFinish()) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		actfundreward afr = ActDataCenter.getData(id, actfundreward.class, pcode);
		task taskexecl = DataCenter.getData(afr.getTaskid(), task.class);
		if(taskexecl == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(!taskbean.isReward()) {
			taskbean.setReward(true);
			taskbean.set();			
		}
		
		//奖励
		ArrayList<Currency> reward = new ArrayList<Currency>();
		
		//免费
		if(!bf.getDrawids().contains(id)) {
			Collection<Currency> curs = Currency.create(afr.getRewardtype1(), afr.getRewardid1(), afr.getRewardnum1());
			reward.addAll(curs);
			
			bf.getDrawids().add(id);
		}
		
		//付费
		if(!bf.getPaydrawids().contains(id) && buy != null) {
			Collection<Currency> curs = Currency.create(afr.getRewardtype2(), afr.getRewardid2(), afr.getRewardnum2());
			reward.addAll(curs);
			
			bf.getPaydrawids().add(id);
		}
		
		am.set();
		
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(reward);
		
		response.setType(type);
		response.addAllDisplay(items);

		//下发道具
		new CurrencyHandle().addCurrency(pcode, CCReason.活动_关卡基金, reward);
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActBarrierFundDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
}
